<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 获取当前角色下最后一个权限id,返回
        let role = {
            "id": 100,
            "roleName": "主管",
            "roleDesc": "技术负责人",
            "children": [
                {
                    "id": 101,
                    "authName": "商品管理",
                    "path": null,
                    "children": [
                        {
                            "id": 102,
                            "authName": "商品列表",
                            "path": null,
                            "children": [
                                {
                                    "id": 103,
                                    "authName": "添加商品",
                                    "path": null,
                                    "children": [
                                        {
                                            "id": 104,
                                            "authName": "删除商品",
                                            "path": null
                                        }
                                    ]
                                },

                            ]
                        }
                    ]
                }
            ]
        };

        //层级不固定，要用递归函数拿出最后一个id
        let arr = [];
        function fn(role) {
            //1.如果没有children证明就是最后一个,把该children对应的当前id推入数组
            if (!role.children) {
                return arr.push(role.id)   //！！！注意arr外面要使用，要返回
            }
            //2.如果有，一直循环
            role.children.forEach(item => {
                return fn(item)   //递归下次要使用，也要返回
            })

        }
        fn(role)
        console.log(arr);
    </script>
</body>

</html>